﻿using System;
using System.Data;
using System.Web;
using System.Web.Security;
using Core.Library;
using DataObject;
using DataObject.Members;
using DataObject.Photo;
using DataObject.Ref;
using DataObject.Vote;

namespace Event.Controls.Album
{
    public partial class ListAlbum : System.Web.UI.UserControl
    {
        #region variables, constants, properties
        private const int _avatarWidth = 40;
        private const int _avatarHeight = 40;
        private const int _albumWidth = 220;
        private const int _albumHeight = 330;

        //
        // 0 = name, 1 = Encode name, 2 = Url, 3 = avatar, 4 = album avatar with a, 5 = album title, 6 = button vote
        //
        private const string LI_FORMAT = @"<li>
                <div class=""stylist-item clearfix relative"">
                    {4}{6}
                </div>
                <a title=""{1}"" href=""{2}"" >
                    {3}
                    <b class=""pl8"">{0}</b> </a>
                <a title=""{1}"" href=""{2}"" class=""sapo"">
                    <span class=""inner"">{5}</span>
                </a>
            </li>";

        private const string BT_VOTE = @"<a href=""javascript:void(0);"" class=""{0} vote-button vote-bt-{2}"" vote=""{3}"" {1}>{3} VOTE{4}</a>";

        #endregion

        internal bool DataBind(DataTable dt, int round)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var isLockVote = RefCache.Ref_GetLockVoteStatus();
                var MemberId = 0;
                string _Email = string.Empty;
                if (HttpContext.Current.Request.IsAuthenticated && isLockVote)
                {
                    var ident = HttpContext.Current.User.Identity as FormsIdentity;
                    if (ident != null)
                    {
                        try
                        {
                            FormsAuthenticationTicket ticket1 = ident.Ticket;
                            var userData = ticket1.UserData.Split('|');
                            _Email = userData[1];
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                else if (HttpContext.Current.Session[Const.AuthenticationSessionName] != null && isLockVote)
                {
                    string[] userData = HttpContext.Current.Session[Const.AuthenticationSessionName].ToString().Split('|');
                    _Email = userData[1];
                }

                if(!string.IsNullOrEmpty(_Email))
                {
                    var o = MembersCache.Members_GetByEmail(_Email);
                    MemberId = o.Member_ID;
                }

                var currentRound = RefCache.Ref_GetCurrentRound();
                var isDisPlayVote = RefCache.Ref_GetVoteDisplayStatus();
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var clsVote = string.Empty;
                    var textVote = string.Empty;
                    var r = dt.Rows[i];

                    var onclick = string.Format("onclick=\"Vote.voteMember({0})\"", r[Constants.Table_Members.Member_ID]);
                    var vote = VoteCache.Members_GetVoteByRound(ConvertUtility.ConvertTo<int>(r[Constants.Table_Members.Member_ID]), round);

                    if (MemberId > 0 && VoteCache.Vote_CheckIsVoteMemberId(MemberId, ConvertUtility.ConvertTo<int>(r[Constants.Table_Members.Member_ID])))
                    {
                        clsVote = "active";
                        textVote = "D";
                        onclick = string.Empty;
                    }


                    if (isLockVote && round == currentRound)
                        clsVote += " hover";
                    else
                        clsVote = string.Empty;

                    var btnVote = isDisPlayVote || round < currentRound
                                      ? isLockVote && round == currentRound
                                            ? string.Format(BT_VOTE, clsVote, onclick,
                                                            r[Constants.Table_Members.Member_ID], vote, textVote)
                                            : string.Format(BT_VOTE, clsVote, string.Empty,
                                                            r[Constants.Table_Members.Member_ID], vote, textVote)
                                      : string.Empty;

                    var albumAvatar = Utils.Photo.BuildPhoto(
                    source:  r[Constants.Table_Members.Member_OriginalAvatar].ToString(),
                    title: r[Constants.Table_Members.Member_FullName].ToString(),
                    href: r[Constants.Table_Members.Url].ToString(),
                    width: _albumWidth,
                    height: _albumHeight,
                    mustThumb: true);

                    var avatar = Utils.Photo.BuildPhoto(
                    source: r[Constants.Table_Members.Member_Avatar].ToString(),
                    title: r[Constants.Table_Members.Member_FullName].ToString(),
                    width: _avatarWidth,
                    height: _avatarHeight,
                    mustThumb: true);

                    //
                    // 0 = name, 1 = Encode name, 2 = Url, 3 = avatar, 4 = album avatar with a, 5 = album title, 6 = button vote
                    //
                    ltrList.Text += string.Format(LI_FORMAT,
                    r[Constants.Table_Members.Member_FullName],
                    r[Constants.Table_Members.EncodeName],
                    r[Constants.Table_Members.Url],
                    avatar,
                    albumAvatar,
                    r[Constants.Table_Members.Member_AlbumTitle],
                    !isLockVote && round > currentRound ? string.Empty : btnVote);
                }
                return true;
            }
            return false;
        }
    }
}